package educar.models;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import educar.db.DbConnection;
import educar.db.JPA;


public class DocenteAuxiliar {
	private static JPA jpa = new JPA();
	private String dni;
	private Integer codigo;
	
	public DocenteAuxiliar (String dni,Integer codigo){
		this.setDni(dni);
		this.setCodigo(codigo);
	}
	
	
	public boolean save() {
		try {
			DocenteAuxiliar.getDocenteAuxiliarByDocenteAuxiliar(dni,codigo.toString());
			return false;
		} catch (datoNotFound e1) {
			String[] columns = {"dni","codigo"};

			PreparedStatement stm = jpa.newRecord("educar_dev.docenteAuxiliar", columns);

			try {
				stm.setString(2, dni);
				stm.setString(1, Integer.toString(codigo));
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}

			jpa.create(stm);
			return true;
		}
	}
	
	public static DocenteAuxiliar getDocenteAuxiliarByDocenteAuxiliar(String dni, String codigo) throws datoNotFound {
		ResultSet rs = null;
		ResultSet rs2 = null;
		DocenteAuxiliar p = null;

		rs = jpa.getByField("docenteAuxiliar", "dni", dni);
		rs2 = jpa.getByField("docenteAuxiliar", "codigo", codigo);

		try {
			if (rs.next()&& rs2.next()) {
				p = new DocenteAuxiliar(rs.getString(1),Integer.parseInt(rs2.getString(2)));
			} else {
				throw new datoNotFound();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return p;
	}

	public void destroy(String dni,String codigo) {
		String query= "DELETE from " + DbConnection.bd + ".correccion"; 
    	query += " Where dni= '" + dni + "' and codigo= '"+codigo+"' ";
		jpa.runUpdate(query);
	}

	
	
	public void setDni(String dni){
		this.dni=dni;
	}
	
	public String getDni(){
		return this.dni;
	}
	
	public void setCodigo(Integer codigo){
		this.codigo=codigo;
	}
	
	public Integer getCodigo(){
		return this.codigo;
	}
	
	
}
